/*!
 * This file implements the interface between the database and the program.
 * @author Mazzer Yannis, Calingarayar Martial
 * @file liteconnector.h
 */
#include <QtSql>
#include <QSqlTableModel>
#include "liteconnector.h"
#include "../types/movies.h"
#include "../types/albums.h"

dbConnector::dbConnector()
{

}

QSqlError dbConnector::addConnection(const QString &driver,
		const QString &dbName,
		const QString &host,
		const QString &user,
		const QString &passwd,
		const QString &nick,
		const QString &comment,
		int port)
{
	db = QSqlDatabase::addDatabase(driver);
	db.setDatabaseName(dbName);

	db.setHostName(host);
	db.setPort(port);
	if(!db.open(user, passwd)) {
		err = db.lastError();
		db = QSqlDatabase();
		QSqlDatabase::removeDatabase(dbName);
	}
	else {
		err = db.lastError();
		this->setText(nick);
		this->setToolTip(comment);
	}
	return err;
}

QSqlTableModel *dbConnector::retrieveAlbums()
{
    QSqlTableModel *albumList = new QSqlTableModel((QWidget *)NULL, this->db);
    albumList->setTable("Albums");
    albumList->setEditStrategy(QSqlTableModel::OnManualSubmit);
    albumList->setSort(0, Qt::AscendingOrder);
    albumList->select();
    return albumList;

}

QSqlTableModel *dbConnector::retrieveMovies()
{
    QSqlTableModel *movieList = new QSqlTableModel((QWidget *)NULL, this->db);
    movieList->setTable("Films");
    movieList->setEditStrategy(QSqlTableModel::OnManualSubmit);
    movieList->setSort(0, Qt::AscendingOrder);
    movieList->select();
    return movieList;

}

QString dbConnector::getNickName()
{
    return this->nickName;

}

void dbConnector::setNickName(QString nickName)
{
    this->nickName = nickName;
    this->setText(nickName);
}

void dbConnector::setComment(QString comment)
{
    this->comment = comment;
}

int dbConnector::getPort()
{
    return this->port;
}

QString dbConnector::getHost()
{
    return this->host;
}
